﻿namespace IBTrader.Indicator.CandleStick
{
    using IBTrader.Indicator.Average;
    using IBTrader.Modules.Read;
    using IBTrader.Charts;

    /// <summary>
    /// The Detrended Price Oscillator (DPO) is an indicator designed to remove trend from price and make it easier to identify cycles. 
    /// DPO does not extend to the last date because it is based on a displaced moving average. 
    /// However, alignment with the most recent is not an issue because DPO is not a momentum oscillator. 
    /// Instead, DPO is used to identify cycles highs/lows and estimate cycle length.
    /// </summary>
    class DPO : Base
    {
        private readonly SMA sma;
        public DPO(Worker worker, int interval = 60) : base(worker, new ChartArgs { Group = 1 }, interval / 2, interval)
        {
            sma = new SMA(worker, interval);
            sma.Hit += (_, line) => Prices.Add(line.Price);
        }
        protected override void Add(object _, Item item)
        {
            if(Prices.Init)
                OnHit(item.CloseLine, item.Close - Prices[(Prices.Index + Prices.Length / 2) % Prices.Length]);
        }
        public override void Add(object sender, Line line)
        {
            base.Add(sender, line);
            sma.Add(sender, line);
        }
    }
}